package com.ruoyi.project.system.member.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.project.jiaqin.companyindustry.domain.CompanyIndustry;
import com.ruoyi.project.jiaqin.companyindustry.service.ICompanyIndustryService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.jiaqin.viprecord.domain.JiaqinBuyvipRecord;
import com.ruoyi.project.jiaqin.viprecord.mapper.JiaqinBuyvipRecordMapper;
import com.ruoyi.project.mobile.utils.DateUtils;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 小程序用户Service业务层处理
 *
 * @author f_baisi
 * @date 2020-06-09
 */
@Service
public class MemberServiceImpl implements IMemberService
{
    @Resource
    private IdGererateFactory idGererateFactory;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private JiaqinBuyvipRecordMapper jiaqinBuyvipRecordMapper;

    /**
     * 查询小程序用户
     *
     * @param id 小程序用户ID
     * @return 小程序用户
     */
    @Override
    public Member selectMemberById(String id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询小程序用户列表
     *
     * @param member 小程序用户
     * @return 小程序用户
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增小程序用户
     *
     * @param member 小程序用户
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setId(UUID.randomUUID().toString().replaceAll("-",""));
        member.setUpdatedTime(new Date());
        member.setOrderNo(idGererateFactory.nextStringId());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改小程序用户
     *
     * @param member 小程序用户
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        return memberMapper.updateMember(member);
    }

    /**
     * 删除小程序用户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String ids)
    {
        return memberMapper.deleteMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除小程序用户信息
     *
     * @param id 小程序用户ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(String id)
    {
        return memberMapper.deleteMemberById(id);
    }

    @Override
    public Member findOneByOpenId(String openid) {
        return this.memberMapper.findOneByOpenId(openid);
    }

    @Override
    public String[] findListPerson(String id) {
        return this.memberMapper.findListPerson(id);
    }

    @Override
    public Integer findOrderSumByIds(String[] personIds) {
        return this.memberMapper.findOrderSumByIds(personIds);
    }

    @Override
    public Double findOrderPriceSumByIds(String[] personIds) {
        return this.memberMapper.findOrderPriceSumByIds(personIds);
    }

    @Override
    public Integer findSumVipByIds(String[] personIds) {
        return this.memberMapper.findSumVipByIds(personIds);
    }

    @Override
    public List<Member> findMemberListPerson(String userId) {
        return this.memberMapper.findMemberListPerson(userId);
    }

    @Override
    public int payResult(JiaqinBuyvipRecord jiaqinBuyvipRecord, Member member, String transactionId) {
        member.setIsJqVip(1);
        member.setJqVipEndTime(DateUtils.getNextYearToDay());//设置钻石VIP过期时间
        jiaqinBuyvipRecord.setState(1);//已支付
        jiaqinBuyvipRecord.setPayAt(new Date());//设置支付时间
        jiaqinBuyvipRecord.setWxTransactionId(transactionId);//设置微信支付流水号
        jiaqinBuyvipRecordMapper.updateJiaqinBuyvipRecord(jiaqinBuyvipRecord);//更改家禽
        return memberMapper.updateMember(member);
    }

    @Override
    public List<Member> searchMemberList(String memberName, Integer province, Integer city, Integer area, String industry, String memberId) {
        return memberMapper.searchMemberList(memberName, province, city, area, industry, memberId);
    }

    @Autowired
    private ICompanyIndustryService companyIndustryService;
    @Autowired
    private IJiaqinFollowMemberService jiaqinFollowMemberService;
    @Override
    public PageInfo<Member> searchMemberListPage(Integer pageNum, Integer pageSize, String memberId, Member member) {
        PageHelper.startPage(pageNum,pageSize);
        List<Member> memberList = memberMapper.searchMemberList(member.getName(), member.getProvince(), member.getCity(), member.getArea(), member.getIndustry(), memberId);
        if (memberList.size() > 0) {
            for (Member ML : memberList) {
                // 行业名称
                CompanyIndustry companyIndustryById = companyIndustryService.selectCompanyIndustryById(ML.getIndustry());
                if (companyIndustryById != null) {
                    ML.setIndustryName(companyIndustryById.getName());
                }
                // 是否收藏
                JiaqinFollowMember memberByMemberIdAndToMemberId = jiaqinFollowMemberService.selectJiaqinFollowMemberByMemberIdAndToMemberId(memberId, ML.getId());
                if (memberByMemberIdAndToMemberId != null) {
                    ML.setIsCollect(1);
                } else {
                    ML.setIsCollect(0);
                }
                // 展示编辑主营还是选择主营
                if (ML.getBusinessType() == 1) {
                    ML.setBusiness(ML.getBusinessName());
                }
                if (ML.getName() == null) {
                    ML.setName(ML.getUsername());
                }
                // 距离
//                Member member1 = memberMapper.selectMemberById(memberId);
//                Member member2 = memberMapper.selectMemberById(ML.getId());
//                if (member1 != null && member2 != null) {
//                    double distance = DistanceUtils.getDistance(member1.getLongitude().doubleValue(), member1.getLatitude().doubleValue()
//                            , member2.getLongitude().doubleValue(), member2.getLatitude().doubleValue());
//                    DecimalFormat df = new DecimalFormat("######0.0");
//                    df.format(distance);
//                    ML.setDistance(df.format(distance));
//                } else {
//                    ML.setDistance("0.1");
//                }
            }
        }
        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        return pageInfo;
    }

    @Override
    public Member selectMemberByOrderOn(String OrderOn) {
        return memberMapper.selectMemberByOrderOn(OrderOn);
    }

    @Override
    public List<Member> selectAllMember() {
        return memberMapper.selectAllMember();
    }

}
